﻿using UnityEditor;
using UnityEngine;
using UnityEngine.UI;

namespace UnityEditor.UI
{
    /// <summary>
    /// HK_Toggle 编辑器拓展
    /// </summary>
    [CustomEditor(typeof(HK_Toggle), true)]
    [CanEditMultipleObjects]
    public class HK_ToggleEditor : ToggleEditor
    {
        SerializedProperty audioType;
        protected override void OnEnable()
        {
            base.OnEnable();
            audioType = serializedObject.FindProperty("audioType");
        }
        public override void OnInspectorGUI()
        {
            base.OnInspectorGUI();
            serializedObject.Update();

            EditorGUILayout.Space(10);
            serializedObject.Update();
            EditorGUILayout.PropertyField(audioType);
            EditorGUILayout.Space();
            serializedObject.ApplyModifiedProperties();
        }
    }
}

//创建HK_Toggle
public class HK_CreateToggle
{
    static Transform parent_TF;

    [MenuItem("GameObject/UI/HK_Toggle", false, 30)]
    static void CreateHKToggle()
    {
        if (Selection.activeGameObject != null)
        {
            parent_TF = Selection.activeGameObject.transform;
        }

        //调用指定路径菜单项
        EditorApplication.ExecuteMenuItem("GameObject/UI/Toggle");

        GameObject hk_Tog = Selection.activeGameObject;

        if (parent_TF != null)
        {
            Undo.SetTransformParent(hk_Tog.transform, parent_TF, "undo");
            parent_TF = null;
        }

        hk_Tog.name = "Toggle_xxx";
        //销毁Toggle组件
        GameObject.DestroyImmediate(hk_Tog.GetComponent<Toggle>(), false);

        hk_Tog.AddComponent<HK_Toggle>();
        hk_Tog.transform.GetComponentInChildren<Text>().text = "Toggle_xxx";
        hk_Tog.transform.GetComponent<HK_Toggle>().isOn = true;
        hk_Tog.transform.GetComponent<HK_Toggle>().targetGraphic = hk_Tog.transform.GetChild(0).GetComponent<Graphic>();
        hk_Tog.transform.GetComponent<HK_Toggle>().graphic = hk_Tog.transform.GetChild(0).GetChild(0).GetComponent<Graphic>();
    }
}